package services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import vos.MenuSearchVo;
import vos.SystemMenuView;

import com.aton.db.SessionFactory;

import models.Operator;
import models.Role;
import models.RoleMenu;
import models.RoleUser;
import models.SystemMenu;
import models.mappers.OperatorMapper;
import models.mappers.RoleMenuMapper;
import models.mappers.RoleUserMapper;

public class RoleMenuService {
	/**
	 * 获取角色列表
	 *
	 * @Title: getRole 
	 * @return
	 * @return: List<Role>
	 */
	public static List<Role> getRole() {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RoleUserMapper mapper = ss.getMapper(RoleUserMapper.class);
            return mapper.getRole();
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取角色菜单
	 *
	 * @Title: getRoleOperator 
	 * @param roleid
	 * @return
	 * @return: List<String>
	 */
	public static List<String> getRoleMenu(String roleid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RoleMenuMapper mapper = ss.getMapper(RoleMenuMapper.class);
            return mapper.getRoleMenu(roleid);
        } finally {
            ss.close();
        }
	}
	/**
	 * 保存
	 *
	 * @Title: saveRoleUser 
	 * @param roleid
	 * @param operList
	 * @return
	 * @return: boolean
	 */
	public static boolean saveRoleMenu(String roleid, Set<String> menuSet) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		List<RoleMenu> listMap=new ArrayList<RoleMenu>();
		Map<String,List<RoleMenu>> smap=new HashMap<String,List<RoleMenu>>();
		if(menuSet!=null&&menuSet.size()>0){
			Iterator it = menuSet.iterator();
			while(it.hasNext()){
				String id=(String) it.next();
				String[] idarr=id.split("-");
				if(StringUtils.isEmpty(idarr[1])){
					continue;
				}
				if("M".equals(idarr[0])){
					RoleMenu menuUser=new RoleMenu(roleid,idarr[1],true);
					listMap.add(menuUser);
				}
				else{
					RoleMenu menuUser=new RoleMenu(roleid,idarr[1],false);
					listMap.add(menuUser);
				}
			}
		}
		else{
			listMap=null;
		}
		smap.put("menuids", listMap);
		try {
			RoleMenuMapper mapper = ss.getMapper(RoleMenuMapper.class);
			mapper.deleteRoleMenu(roleid);
			if(listMap!=null){
				mapper.saveRoleMenu(smap);
			}
            ss.commit();
        }catch(Exception e){
        	ss.rollback();
        	e.printStackTrace();
        	return false;
        }finally {
            ss.close();
        }
		return true;
	}
	/**
	 * 获取菜单
	 *
	 * @Title: getUseMenu 
	 * @return
	 * @return: List<SystemMenuView>
	 */
	public static List<SystemMenu> getUseMenu() {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RoleMenuMapper mapper = ss.getMapper(RoleMenuMapper.class);
            return mapper.getUseMenu();
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取子菜单
	 *
	 * @Title: getMenus 
	 * @param menuid 菜单id
	 * @return
	 * @return: List<SystemMenu>
	 */
	public static List<SystemMenu> getMenus(String menuid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RoleMenuMapper mapper = ss.getMapper(RoleMenuMapper.class);
            return mapper.getMenus(menuid);
        } finally {
            ss.close();
        }
	}
}
